package com.yangff.controller;



import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangff.anno.LogExecuteTime;
import com.yangff.entity.user.entity.UserAsset;
import com.yangff.entity.user.entity.dto.UserAssetDTO;
import com.yangff.entity.user.entity.vo.RiskAssessmentVo;
import com.yangff.entity.user.entity.vo.UserAssetVo;
import com.yangff.entity.vo.ResultVo;
import com.yangff.service.IUserAssetService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 存储用户的基金资产信息 前端控制器
 * </p>
 *
 * @author yangff
 * @since 2025-01-14
 */
@LogExecuteTime
@RestController
@RequestMapping("/user-asset")
@RequiredArgsConstructor
public class UserAssetController {
    private final IUserAssetService userAssetService;



    @GetMapping("/{code}")
    public List<UserAssetVo> getUserAssetByCode(@PathVariable String code) {
        return userAssetService.getUserAssetByCode(code);
    }

    @GetMapping("/asset/{account}")
    public Page<UserAssetVo> getUserAssetByAccount(@PathVariable String account, @PageableDefault Pageable pageable) {
        // 创建 MyBatis-Plus 的 Page 对象
        Page<UserAsset> page = new Page<>(pageable.getPageNumber() + 1, pageable.getPageSize());

        // 使用 MyBatis-Plus 的分页查询
        IPage<UserAsset> userAssetPage = userAssetService.page(page, new LambdaQueryWrapper<UserAsset>().eq(UserAsset::getAccount, account));

        // 将 UserAsset 转换为 UserAssetVo
        Page<UserAssetVo> userAssetVoPage = new Page<>(userAssetPage.getCurrent(), userAssetPage.getSize(), userAssetPage.getTotal());
        userAssetVoPage.setRecords(BeanUtil.copyToList(userAssetPage.getRecords(), UserAssetVo.class));

        return userAssetVoPage;
    }

    @Cacheable(value = "userasset", key = "#account")
    @GetMapping("/asset/{account}/all")
    public List<UserAssetVo> getAllUserAssetByAccount(@PathVariable String account) {
        // 使用 MyBatis-Plus 的查询
        List<UserAsset> userAssetList = userAssetService.list(new LambdaQueryWrapper<UserAsset>().eq(UserAsset::getAccount, account));

        // 将 UserAsset 转换为 UserAssetVo
        List<UserAssetVo> userAssetVoList = BeanUtil.copyToList(userAssetList, UserAssetVo.class);

        return userAssetVoList;
    }





    //提交用户变更的资产信息，如果发现已存在相同的，就把原记录删除合并
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "userasset", key = "#userAssetDTO.account")
    @PostMapping
    public ResultVo changeUserAssetAmount(@RequestBody UserAssetDTO userAssetDTO) {
        return userAssetService.changeUserAssetAmountAndShares(userAssetDTO);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "userasset", key = "#userAssetDTO.account")
    @PutMapping
    public ResultVo updateUserAssetByCode(@RequestBody UserAssetDTO userAssetDTO) {
        UserAsset userAsset = BeanUtil.copyProperties(userAssetDTO, UserAsset.class);
        userAssetService.update(userAsset , new LambdaQueryWrapper<UserAsset>()
                .eq(UserAsset::getAccount,userAssetDTO.getAccount())
                .eq(UserAsset::getCode,userAssetDTO.getCode()));
        return ResultVo.success();
    }


    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "userasset", key = "#userAssetDTO.account")
    @PutMapping("/status")
    public ResultVo updateUserAssetStatusByAccount(@RequestBody UserAssetDTO userAssetDTO) {
        UserAsset userAsset = BeanUtil.copyProperties(userAssetDTO, UserAsset.class);
        LambdaUpdateWrapper <UserAsset> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserAsset::getAccount,userAssetDTO.getAccount())
                .eq(UserAsset::getCode,userAssetDTO.getCode())
                .set(UserAsset::getStatus,userAssetDTO.getStatus());
        return ResultVo.success();
    }


    //当管理员删掉某个用户的某个基金，就删除该用户的该基金的持有
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "userasset", allEntries = true)
    @DeleteMapping("/{code}")
    public ResultVo deleteUserAssetByCode(@PathVariable String code) {
        userAssetService.remove(new LambdaQueryWrapper<UserAsset>().eq(UserAsset::getCode,code));
        return ResultVo.success();
    }

    @GetMapping("/riskassessment/{account}")
    public RiskAssessmentVo getRiskAssessmentByAccount(@PathVariable String account) {
        return userAssetService.getRiskAssessmentByAccount(account);

    }




}
